package com.atguigu.tingshu.model.search;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.CompletionField;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.suggest.Completion;

import java.io.Serial;
import java.io.Serializable;

/**
 * 专辑标题自动补全索引
 * 专门用于存储专辑标题的completion类型字段，优化自动补全性能
 */
@Data
@Document(indexName = "album_suggest")
@JsonIgnoreProperties(ignoreUnknown = true)
public class AlbumSuggestIndex implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    // 专辑ID作为文档ID
    @Id
    private Long albumId;

    // 专辑标题的自动补全字段
    @CompletionField(maxInputLength = 100)
    private Completion suggest;

    /**
     * 构造方法：根据专辑ID和标题创建补全索引
     */
    public AlbumSuggestIndex() {
    }

    public AlbumSuggestIndex(Long albumId, String albumTitle) {
        this.albumId = albumId;
        this.suggest = new Completion(new String[]{albumTitle});
    }
}
